package cn.rxmind.dlicense.core.validator;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.rxmind.dlicense.core.base.DLicenseContent;
import cn.rxmind.dlicense.core.rule.ActiveTimeRule;
import lombok.Setter;

import java.util.Date;

/**
 * 有效期校验
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/16 3:17 下午
 */
public class LicenseActiveTimeValidator extends AbstractExitJVMLicenseValidator {

    @Override
    public String description() {
        return "证书有效期校验";
    }

    @Override
    protected ValidateResult validateResult(DLicenseContent content) {

        ActiveTimeRule rule = content.getLicenseActiveTimeRule();
        LicenseActiveTimeValidateResult result = new LicenseActiveTimeValidateResult();
        result.setRule(rule);
        result.setValid(rule.test(new Date()));
        return result;
    }

    @Setter
    static class LicenseActiveTimeValidateResult implements ValidateResult{

        private ActiveTimeRule rule;
        private boolean valid;

        @Override
        public Class<LicenseActiveTimeValidator> getLicenseValidator() {
            return LicenseActiveTimeValidator.class;
        }

        @Override
        public boolean valid() {
            return this.valid;
        }

        @Override
        public String getDescription() {

            return valid() ? "valid" : String.format("证书使用起止时间为：%s,现在是：%s",
                    DateUtil.format(rule.getNotBefore(), DatePattern.NORM_DATE_PATTERN),
                    DateUtil.format(rule.getNotAfter(), DatePattern.NORM_DATE_PATTERN));
        }
    }

}
